/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018年1月2日 下午4:42:56
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */  
package com.itttq.gshop.pbpm.provider;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import com.itttq.gshop.domain.po.SmsSendPo;
import com.itttq.gshop.pbpm.cmpt.mybatis.CustomerSQL;

/**   
 * @ClassName:  SmsSendPoSqlProvider
 * @author: 迷蝶
 * @date:   2018年1月2日 下午4:42:56 
 * @Description: TODO
 *     
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
public class SmsSendSqlProvider {
	public static final String TABLE_NAME = "sms_send";

	/**
	 * 查找的SQL语句组装<br>
	 * 约定：分页两个参数为：start - 起始位置； size- 每页数量
	 * 
	 * @param smsSend
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findSmsSends(final SmsSendPo smsSend) {
		return new CustomerSQL() {{
			SELECT("content,create_time,creator_id,id,mobile,receive_time,request_time,retry,source,status,template_id,template_name,update_time,updater_id ");
			FROM(TABLE_NAME);
			
			if (StringUtils.isNotBlank(smsSend.getContent())) {
				WHERE("content=#{content}");
			}
			if (null != smsSend.getCreatorId()) {
				WHERE("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(smsSend.getMobile())) {
				WHERE("mobile=#{mobile}");
			}
			if (null != smsSend.getReceiveTime()) {
				WHERE("receive_time=#{receiveTime}");
			}
			if (null != smsSend.getRequestTime()) {
				WHERE("request_time=#{requestTime}");
			}
			if (null != smsSend.getRetry()) {
				WHERE("retry=#{retry}");
			}
			if (StringUtils.isNotBlank(smsSend.getSource())) {
				WHERE("source=#{source}");
			}
			if (null != smsSend.getStatus()) {
				WHERE("status=#{status}");
			}
			if (null != smsSend.getTemplateId()) {
				WHERE("template_id=#{templateId}");
			}
			if (StringUtils.isNotBlank(smsSend.getTemplateName())) {
				WHERE("template_name=#{templateName}");
			}
			if (null != smsSend.getUpdaterId()) {
				WHERE("updater_id=#{updaterId}");
			}
			if(StringUtils.isNotBlank(smsSend.getSortField())) {
				ORDER_BY(smsSend.getSortField() + (StringUtils.isNotBlank(smsSend.getSortMethod()) ? "desc" : smsSend.getSortField()));
			} else {
				ORDER_BY("id desc");
			}
			if(null != smsSend.getStart() && null != smsSend.getSize()) {
				LIMIT(smsSend.getStart(), smsSend.getSize());
			}
		}}.toString();
	}
	
	/**
	 * 查找记录总数的SQL语句组装
	 * 
	 * @param smsSend
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findSmsSendCount(final SmsSendPo smsSend) {
		return new SQL() {{
			SELECT("count(*)");
			FROM(TABLE_NAME);
			if (StringUtils.isNotBlank(smsSend.getContent())) {
				WHERE("content=#{content}");
			}
			if (null != smsSend.getCreatorId()) {
				WHERE("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(smsSend.getMobile())) {
				WHERE("mobile=#{mobile}");
			}
			if (null != smsSend.getReceiveTime()) {
				WHERE("receive_time=#{receiveTime}");
			}
			if (null != smsSend.getRequestTime()) {
				WHERE("request_time=#{requestTime}");
			}
			if (null != smsSend.getRetry()) {
				WHERE("retry=#{retry}");
			}
			if (StringUtils.isNotBlank(smsSend.getSource())) {
				WHERE("source=#{source}");
			}
			if (null != smsSend.getStatus()) {
				WHERE("status=#{status}");
			}
			if (null != smsSend.getTemplateId()) {
				WHERE("template_id=#{templateId}");
			}
			if (StringUtils.isNotBlank(smsSend.getTemplateName())) {
				WHERE("template_name=#{templateName}");
			}
			if (null != smsSend.getUpdaterId()) {
				WHERE("updater_id=#{updaterId}");
			}
		}}.toString();
	}
	
	/**
	 * 根据ID查找的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findSmsSendById(final Integer id) {
		return new SQL() {{
			SELECT("*");
			FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 增加的SQL语句组装
	 * 
	 * @param smsSend
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String addSmsSend(final SmsSendPo smsSend) {
		return new SQL() {{
			INSERT_INTO(TABLE_NAME);
			if (StringUtils.isNotBlank(smsSend.getContent())) {
				VALUES("content","#{content}");
			}
			if (null != smsSend.getCreatorId()) {
				VALUES("creator_id","#{creatorId}");
			}
			if (StringUtils.isNotBlank(smsSend.getMobile())) {
				VALUES("mobile","#{mobile}");
			}
			if (null != smsSend.getReceiveTime()) {
				VALUES("receive_time","#{receiveTime}");
			}
			if (null != smsSend.getRequestTime()) {
				VALUES("request_time","#{requestTime}");
			}
			if (null != smsSend.getRetry()) {
				VALUES("retry","#{retry}");
			}
			if (StringUtils.isNotBlank(smsSend.getSource())) {
				VALUES("source","#{source}");
			}
			if (null != smsSend.getStatus()) {
				VALUES("status","#{status}");
			}
			if (null != smsSend.getTemplateId()) {
				VALUES("template_id","#{templateId}");
			}
			if (StringUtils.isNotBlank(smsSend.getTemplateName())) {
				VALUES("template_name","#{templateName}");
			}
			if (null != smsSend.getUpdaterId()) {
				VALUES("updater_id","#{updaterId}");
			}
		}}.toString();
	}

	/**
	 * 修改的SQL语句组装
	 * 
	 * @param smsSend
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String updateSmsSend(final SmsSendPo smsSend) {
		return new SQL() {{
			UPDATE(TABLE_NAME);
			
			if (StringUtils.isNotBlank(smsSend.getContent())) {
				SET("content=#{content}");
			}
			if (null != smsSend.getCreatorId()) {
				SET("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(smsSend.getMobile())) {
				SET("mobile=#{mobile}");
			}
			if (null != smsSend.getReceiveTime()) {
				SET("receive_time=#{receiveTime}");
			}
			if (null != smsSend.getRequestTime()) {
				SET("request_time=#{requestTime}");
			}
			if (null != smsSend.getRetry()) {
				SET("retry=#{retry}");
			}
			if (StringUtils.isNotBlank(smsSend.getSource())) {
				SET("source=#{source}");
			}
			if (null != smsSend.getStatus()) {
				SET("status=#{status}");
			}
			if (null != smsSend.getTemplateId()) {
				SET("template_id=#{templateId}");
			}
			if (StringUtils.isNotBlank(smsSend.getTemplateName())) {
				SET("template_name=#{templateName}");
			}
			if (null != smsSend.getUpdaterId()) {
				SET("updater_id=#{updaterId}");
			}
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 根据ID删除的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String delSmsSend(final Integer id) {
		return new SQL() {{
			DELETE_FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
}
